package com.rjet.view_demo1;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

public class Demo2Adapter extends BaseQuickAdapter<DemoEntity, BaseViewHolder> {

    private final List<DemoEntity> data;

    interface ChooseListener{

        void choose(Integer index);
        void finishEdit(String num, Integer tag);
        void minus(Integer tag);
        void plus(Integer tag);
    }

    private ChooseListener chooseListener;

    public void setChooseListener(ChooseListener chooseListener) {
        this.chooseListener = chooseListener;
    }

    private final Context context;

    public Demo2Adapter(Context context, int layoutResId, @Nullable List<DemoEntity> data) {
        super(layoutResId, data);
        this.context = context;
        this.data = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, DemoEntity item) {

        LinearLayout par = helper.getView(R.id.p);

        TextView tv = helper.getView(R.id.name);
        tv.setText(item.getName());
        tv.setTag(101+item.getId());

        TextView num = helper.getView(R.id.num);
        num.setText(item.getContent());
        num.setTag(201+item.getId());

        if (item.getSelected() == 0){
            par.setBackgroundColor(this.context.getResources().getColor(R.color.colorWhite));
        }else {
            par.setBackgroundColor(this.context.getResources().getColor(R.color.colorAccent));
        }

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d(TAG, "onClick: " + v.getTag());
                chooseListener.choose((Integer) v.getTag());
            }
        });

        num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d(TAG, "onClick: " + num.getTag());
                Demo2Popup demo2Popup = new Demo2Popup(context, num.getTag());
                demo2Popup.showPopupWindow();

                //编辑完数量
                demo2Popup.setNumEditListener(new Demo2Popup.NumEditListener() {
                    @Override
                    public void finishEdit(String num, Integer tag) {

                        Log.d(TAG, "finishEdit: " + num + " tag: " + tag);
                        chooseListener.finishEdit(num, tag);

                        demo2Popup.dismiss(); //关闭弹层
                    }

                    @Override
                    public void minus(Integer tag) {

                        chooseListener.minus(tag);
                    }

                    @Override
                    public void plus(Integer tag) {

                        chooseListener.plus(tag);
                    }
                });
            }
        });

    }
}
